package org.tio.site.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.site.init.JfinalInit;
import org.tio.site.model.Donate;
import org.tio.site.model.DonateTitle;
import org.tio.site.utils.EhcacheConst;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.ehcache.CacheKit;

/**
 * @author tanyaowu
 * 2017年7月22日 上午10:44:55
 */
public class DonateService {
	private static Logger log = LoggerFactory.getLogger(DonateService.class);
	public static final DonateService me = new DonateService();

	/**
	 * @param args
	 * @author tanyaowu
	 */
	public static void main(String[] args) {
		JfinalInit.init();

		Page<Donate> page = DonateService.me.page(1, 10);
		System.out.println(page);
	}

	private final Donate dao = new Donate().dao();
	
	private final DonateTitle donateTitleDao = new DonateTitle().dao();

	/**
	 *
	 * @author tanyaowu
	 */
	public DonateService() {
	}

	@SuppressWarnings("unchecked")
	public Page<Donate> page(int pageNumber, int pageSize) {
		String cacheName = EhcacheConst.CacheName.T_5;
		String cacheKey = "donate_page" + "_" + pageNumber + "_" + pageSize;
		Object obj = CacheKit.get(cacheName, cacheKey);
		if (obj != null) {
			return (Page<Donate>) obj;
		}

		SqlPara sqlPara = dao.getSqlPara("donate.page");
		Page<Donate> ret = dao.paginate(pageNumber, pageSize, sqlPara);
		CacheKit.put(cacheName, cacheKey, ret);
		return ret;
	}
	
	/**
	 * 捐赠总金额
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 * @author tanyaowu
	 */
	public Double totalAmount() {
		String cacheName = EhcacheConst.CacheName.T_5;
		String cacheKey = "donate_totalAmount";
		Object obj = CacheKit.get(cacheName, cacheKey);
		if (obj != null) {
			return (Double) obj;
		}

		Double ret = Db.queryDouble("select sum(amount) from donate");
		CacheKit.put(cacheName, cacheKey, ret);
		return ret;
	}
	
	public static <V> List<V> randomList(List<V> sourceList) {  
	    if (sourceList == null || sourceList.size() == 0) {  
	        return sourceList;  
	    }  
	    List<V> random = new ArrayList<V>(sourceList.size());  
	    do {  
	        int index = Math.abs(new Random().nextInt(sourceList.size()));  
	        random.add(sourceList.remove(index));  
	  
	    } while (sourceList.size() > 0);  
	  
	    return random;  
	  
	}  
	
	
	static long requestCount = 1;
	
	public List<DonateTitle> selectDonateTitle() {
		
		long xx = (requestCount++) % 3;
		xx++;
		
		String cacheName = EhcacheConst.CacheName.T_5;
		String cacheKey = "donate_selectDonateTitle" + xx;
		List<DonateTitle> ret = (List)CacheKit.get(cacheName, cacheKey);
		if (ret != null) {
			return ret;
		}

		ret = donateTitleDao.find("SELECT * FROM donate_title  where status = 1 and orderno=?  order by orderno limit 0,3", xx);
		
		ret = randomList(ret);
		
		CacheKit.put(cacheName, cacheKey, ret);
		return ret;
	}
	
	//
}
